CommonUtils.regNamespace("com.im.login");

//com.im.login.invoke = function(){

function _init() {

    var flag = getUrlParam("flag");
    if(flag != undefined && flag == 'loginOtherIp'){
        //另一IP登录
        toastr.error("检测到您的账号在其他地方登陆，请重新登录!!!");
    }

    $("#CreateCheckCode").click(function () {
        _getCode();
    });

    $("#loginBtn").click(function () {
        _login();
    });

    _getCode();
};

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

function _getCode() {
    var url = "/InfoManageWeb/loginController/getValidCode";
    $("#CreateCheckCode").attr('src', url + '?nocache=' + new Date().getTime());
};

function _login() {
    var name = $("#name").val();
    var password = $("#password").val();
    var validCode = $("#validCode").val();
    if (name == "") {
        toastr.warning("用户名不能为空");
        return;
    }

    if (password == "") {
        toastr.warning("密码不能为空");
        return;
    }

    if (validCode == "") {
        toastr.warning("验证码不能为空");
        com.im.common.confirm("验证码不能为空", null, null);
        return;
    }
    var param = {};
    param.staffCode = name;
    param.password = password;
    param.validCode = validCode;
    var client = new com.im.common.ajax();
    var result = client.callAsJson("loginController/validate", param);
    if (!result.success) {
        toastr.error(result.resultMsg);
        _getCode();
        $("#validCode").val('');
    } else {
        var date = new Date();
        var minutes = 30;
        date.setTime(date.getTime() + (minutes * 60 * 1000));
        $.cookie("name", name, {expires: date, path: '/'});
        $.cookie("password", password, {expires: date, path: '/'});
        document.location.href = "../userController/index";
    }
//      $.ajax({
//        url: "/loginController/validate",
//        data: param,
//        method:'post',
//        success: function(data){
//          console.log(data);
//          var result=JSON.parse(data);
//          if(result.resultCode=="-1"){
//            $.alert(result.resultMsg,2000,{type:0});
//            _getCode();
//            $("#validCode").val('');
//
//          }else{
//           var date = new Date();
//           var minutes = 30;
//            date.setTime(date.getTime()+(minutes * 60 * 1000));
//            $.cookie("name", name,{expires:date,path:'/'});
//            $.cookie("password",password,{expires:date,path:'/'});
//            document.location.href = "/userController/index";
//          }
//
//        }});
};

//   return{
//     init: _init(),
//     getCode : _getCode(),
//   }
//
// }();

$(function () {
    _init();
    _getCode();

    //回车事件
    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            _login();
        }
    });
//  com.im.login.invoke.init();
});

